package com.angelhack.metrosocial.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.angelhack.metrosocial.R;
import com.angelhack.metrosocial.adapters.ComentariosAdapter;
import com.angelhack.metrosocial.utils.ComentariosEstacion;
import com.angelhack.metrosocial.utils.Log;
import com.angelhack.metrosocial.utils.Post;
import com.angelhack.metrosocial.utils.Utilities;

public class ComentariosActivity extends SherlockActivity{

	private ListView lv;
	private ProgressBar progress;
	private ArrayList<ComentariosEstacion> array;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_comentarios);
		lv = (ListView)findViewById(R.id.listaComentarios);
		progress = (ProgressBar)findViewById(R.id.progressLista);
		int value = getIntent().getIntExtra("id", 0);
		String title = getIntent().getExtras().getString("title");
		getSupportActionBar().setTitle(title);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		new AsyncGetComments().execute(String.valueOf(value));
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}

	private class AsyncGetComments extends AsyncTask<String, Void, Boolean>
	{

		@SuppressWarnings("unchecked")
		@Override
		protected Boolean doInBackground(String... params) {
			String id = params[0];
			Post post = new Post();
			@SuppressWarnings("rawtypes")
			ArrayList dataValues = new ArrayList();
			dataValues.add("id_station");
			dataValues.add(id);
			try {
				Log.v("Post Coment: "+ id);
				JSONArray jsonValues = 	post.getServerData(dataValues, Utilities.HTPP_POST_GET_COMMENT);
				if(jsonValues!=null)
				{
					array = new ArrayList<ComentariosEstacion>();
					for (int i = 0; i < jsonValues.length(); i++) {
						JSONObject value =  jsonValues.getJSONObject(i);
						ComentariosEstacion coment;
						coment= new ComentariosEstacion(value.getString("coment")
								, value.getString("user"), 
								value.getString("img_b64"), 
								value.getString("id"),
								Integer.valueOf(value.getString("status")));
						if(coment.getUser().length()==0 && coment.getImg().equals("0") && coment.getComentarios().length()==0)
						{
							continue;
						}
						array.add(coment);
					}
					
					return true;
				}
				
			} catch (Exception e) {
				Log.v("Error al mandar POST GET Commentarios: "+e.getMessage());
			}
			
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			progress.setVisibility(View.GONE);
			if(result)
			{
				ComentariosAdapter adapter = new ComentariosAdapter(getApplicationContext(), R.layout.list_comentario_single, array);
				lv.setAdapter(adapter);
			}
			
			
		}
		
	}


}
